package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.*;
import com.project.mapper.*;
import com.project.service.IPscSupplyService;
import com.project.service.IPutInStorageService;
import com.project.service.IRecordService;
import com.project.util.SnowIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class PutInStorageServiceImpl implements IPutInStorageService {
    @Autowired
    private IInventoryInfoMapper IInventoryInfoMapper;//库存商品明细表
    @Autowired
    private IInventoryMapper iInventoryMapper;//库存商品表
    @Autowired
    private IRecordService recordService;

    @Autowired
    private ISupplyInfoMapper ISupplyInfoMapper;//供货批次信息表
    @Autowired
    private IInProductMapper IInProductMapper;//供货商品信息
    @Autowired
    private ICusReturnMapper ICusReturnMapper;//退货订单信息
    @Autowired
    private IOrderReturnProductMapper IOrderReturnProductMapper;//退货订单中的商品信息
    @Autowired
    private IPscSupplyService pscSupplyService;//调用供应商接口修改状态

    public void te(){
//        调用供应商的接口修改订单状态
          pscSupplyService.updateStatus("P1683774390857",2);

    }
    @Override
    public void goodsRejected(CusReturnBean cusReturnBean) {
        //添加退货订单信息
        CusReturnBean cusReturnBean1 = new CusReturnBean(
                cusReturnBean.getOrderNum(), cusReturnBean.getTotalPrice(), cusReturnBean.getReturnDate(),
                "未入库",cusReturnBean.getCustomerName());
        ICusReturnMapper.insert(cusReturnBean1);

        //将退货订单的商品循环添加到 退货订单商品表中
        List<OrderReturnProductBean> productBeanList = cusReturnBean.getOrderReturnProductBeans();
        for (int i = 0; i < productBeanList.size(); i++) {
            IOrderReturnProductMapper.insert(productBeanList.get(i));
        }
    }

    @Override
    public IPage<CusReturnBean> findByRejected(int pageNO) {

        return ICusReturnMapper.selectPage(new Page<>(pageNO,3),null);
    }

    @Override
    public IPage<OrderReturnProductBean> findByRejectedId(int pageNO, String id) {
        QueryWrapper qw=new QueryWrapper<>();
        qw.eq("fk_cusReturnId",id);
        return IOrderReturnProductMapper.selectPage(new Page<>(pageNO,3),qw);
    }



    @Override
    public boolean joinRejectedToStorage(int id) {

        CusReturnBean cusReturn =ICusReturnMapper.selectById(id);
        if (cusReturn.getAuditStatus().equals("已入库")){
            return false;
        }else {
            //将此订单状态改为已入库
            CusReturnBean cusReturnBean = ICusReturnMapper.selectById(id);
            cusReturnBean.setAuditStatus("已入库");
            ICusReturnMapper.updateById(cusReturnBean);


            QueryWrapper<OrderReturnProductBean> qw = new QueryWrapper<>();
            qw.eq("fk_cusReturnId", id);
            List<OrderReturnProductBean> returnList = IOrderReturnProductMapper.selectList(qw);
            for (int i = 0; i < returnList.size(); i++) {
                OrderReturnProductBean orderReturnProductBean = returnList.get(i);
                String productId = orderReturnProductBean.getProductId();
                //
                QueryWrapper<InventoryInfoBean> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("productId", productId);
                InventoryInfoBean inventoryInfoBean = IInventoryInfoMapper.selectOne(queryWrapper);
                //根据退货数量添加库存商品数量
                int num = inventoryInfoBean.getProductInfoNum() + returnList.get(i).getProductNum();
                inventoryInfoBean.setProductInfoNum(num);//添加数量
                IInventoryInfoMapper.updateById(inventoryInfoBean);//更新
            }

            return true;
        }


    }

    //    @Override
//    public void saveTenderInfo(TrackBean trackBean) {
//        trackBean.setStatus("进行中");
//        trackMapper.insert(trackBean);
//    }
    @Override
    public void SupplyGoods(SupplyInfoBean supplyInfoBean) {//供应商供货订单表，里面有供货商品集合
       int allNum=0;
        //将供货订单表中的商品循环添加到供货商品表里。
        List<InProductBean> productBeanList = supplyInfoBean.getInProductBeans();
        for (int i = 0; i < productBeanList.size(); i++) {
            allNum+=productBeanList.get(i).getProductNum();
            IInProductMapper.insert(productBeanList.get(i));
        }

        //添加供货订单到供货批次订单表里面
        SupplyInfoBean supplyInfoBean1 = new SupplyInfoBean(
                supplyInfoBean.getSupplyRefId(),
                supplyInfoBean.getSupplyId(),
                supplyInfoBean.getSupplier(),
                allNum,
                allNum,
                supplyInfoBean.getAllMoney(),
                supplyInfoBean.getInDate(),"未入库");

        ISupplyInfoMapper.insert(supplyInfoBean1);
    }

    @Override
    public IPage<SupplyInfoBean> findBySupplyAll(int pageNO) {
        QueryWrapper<SupplyInfoBean> query = new QueryWrapper<>();

        return ISupplyInfoMapper.selectPage(new Page<>(pageNO,3),query);
    }

    @Override
    public IPage<InProductBean> findProductBySupplyId(int pageNO, int supplyId) {
        SupplyInfoBean supplyInfoBean = ISupplyInfoMapper.selectById(supplyId);
        QueryWrapper<InProductBean> query = new QueryWrapper<>();
        query.eq("trackId",supplyInfoBean.getSupplyId());
        return IInProductMapper.selectPage(new Page<>(pageNO,3),query);
    }

    @Override
    public boolean joinSupplyToStorage(int id) {



        //根据供货订单id将状态改成已入库
        SupplyInfoBean supplyInfoBean = ISupplyInfoMapper.selectById(id);
        if (!supplyInfoBean.getStatus().equals("已入库")){





            QueryWrapper<InProductBean> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("trackId", supplyInfoBean.getSupplyId());
            List<InProductBean> list = IInProductMapper.selectList(queryWrapper);
//        //将供应商供货订单的商品循环添加到 库存表中
            for (int i = 0; i < list.size(); i++) {

                //将商品通过商品名和商品类型添加进库存表，如果有相同的数量怎加，如果没有新增一条
                InProductBean productbean = list.get(i);
                QueryWrapper<InventoryBean> qw = new QueryWrapper<>();
                qw.eq("commodityName", productbean.getProductName());
//                qw.eq("type", productbean.getProductType());
                InventoryBean inventoryBean = iInventoryMapper.selectOne(qw);
                if (inventoryBean != null) {
//                    inventoryBean.setRemindNum(inventoryBean.getRemindNum() + productbean.getProductNum());
                    inventoryBean.setSumNum(inventoryBean.getSumNum() + productbean.getProductNum());
                    iInventoryMapper.updateById(inventoryBean);//修改数量后更新
                    String proId= SnowIdUtils.uniqueLongHex();
                    //添加商品信息到详情表中。
                    InventoryInfoBean recordInfo = new InventoryInfoBean(inventoryBean.getProductNum(),
                            proId, productbean.getProductName(),
                            productbean.getColor(), productbean.getSize(), productbean.getProductNum(),
                            productbean.getInPrice(), supplyInfoBean.getSupplier());

                    IInventoryInfoMapper.insert(recordInfo);
                    recordService.addInRecord(recordInfo,productbean.getProductNum(),"入库");
                } else {
                    //添加商品到库存表
                    String proId= SnowIdUtils.uniqueLongHex();
                    iInventoryMapper.insert(new InventoryBean(proId,
                            productbean.getProductName(), productbean.getProductType(), productbean.getProductNum(),
                            productbean.getProductNum()));


                    //添加商品信息到详情表中。
                    InventoryInfoBean recordInfo2 = new InventoryInfoBean(proId,
                            SnowIdUtils.uniqueLongHex(),  productbean.getProductName(),
                            productbean.getColor(), productbean.getSize(), productbean.getProductNum(),
                            productbean.getInPrice(), supplyInfoBean.getSupplier());
                    IInventoryInfoMapper.insert(recordInfo2);
                    recordService.addInRecord(recordInfo2,productbean.getProductNum(),"入库");

                }
            }
//            调用供应商的接口修改订单状态
            pscSupplyService.updateStatus(supplyInfoBean.getSupplyId(),2);

            supplyInfoBean.setStatus("已入库");
            ISupplyInfoMapper.updateById(supplyInfoBean);
            return true;
        }else {
            return false;
        }



    }
}
